package cn.yanglq.Rss.web.controller;

import cn.yanglq.Rss.web.WebConsts;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by yanglq on 2017/6/13.
 */
public class BaseController {
    /**
     * 得到用户的token,现在的实现是从session中得到loginid
     *
     * @return
     */
    protected String findCurrentLoginid() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) ra)
                .getRequest();
        return (String) request.getSession().getAttribute(
                WebConsts.SESSION_USERID);
    }

    /**
     * 得到用户的token,现在的实现是从session中得到usertype
     *
     * @return
     */
    protected short findCurrentUserType() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) ra)
                .getRequest();
        return (Short) request.getSession().getAttribute(
                WebConsts.SESSION_USERTYPE);
    }

    /**
     * 得到user-agent
     *
     * @return
     */
    protected String findUserAgent() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) ra)
                .getRequest();
        return request.getHeader("User-Agent");
    }
}
